angular.module('goods-edit', ['img-picker','div-slide-in','head-toast'])

.controller 'GoodsEditCtrl', ($scope, $state,$ionicLoading, $http, $ionicHistory, $stateParams, newAddress, goodsInfo,$ionicViewSwitcher,$ionicPopup,userData,$log,showToast) ->
    console.log goodsInfo.goods
    console.log goodsInfo.goods.isupk
    getareaData = ->
        $http.get('/seller/',params:{action:'areadata'})
        .success (resp) ->

            if resp.status == '0'
                $scope.areaData = resp.body
                console.log $scope.areaData
                $scope.provinceData = $scope.areaData[0].province
                $scope.cityData = $scope.areaData[1].city
                $scope.currentCity = []
                _.each $scope.cityData,(city) ->
                    if $scope.goods.province == city.provinceid
                        $scope.currentCity.push city
                console.log $scope.currentCity
        .error ->
            showToast '网络异常,请检查网络是否连接'


    getSort = ->
        $http.get '/updshop/',params:{action:'productsort',userid:userData.getToken()}
        .success (resp) ->
            if resp.status == '0'
                $scope.Allsort = resp.body
                console.log $scope.Allsort
                _.each $scope.Allsort,(first)->
                    if first.firstsortid == $scope.goods.firstsort
                        $scope.secondSort = first.childsort
                console.log $scope.secondSort
                _.each $scope.secnondSort,(second)->
                    if second.secondsortid == $scope.goods.secondsort
                        $scope.thirdSort = second.childsort
                console.log $scope.thirdSort
        .error (e)->
            showToast '网络异常,请检查网络是否连接'

    _.extend $scope,
        goods:
            action:'saveproduct'
            userid:userData.getToken()
            id:goodsInfo.goods.id
            title:goodsInfo.goods.title
            productno:goodsInfo.goods.productno
            saleprice:goodsInfo.goods.price
            productsku:goodsInfo.goods.stock
            mainimg:goodsInfo.goods.imgs
            txtimg:goodsInfo.goods.images
            isupk:goodsInfo.goods.isupk
            unitage:goodsInfo.goods.unitage #计量
            brand:goodsInfo.goods.brand #品牌
            countries:goodsInfo.goods.countries  #原产国
            model:goodsInfo.goods.model  #规格型号
            conditions:goodsInfo.goods.conditions  #规格参数
            service:goodsInfo.goods.service  #售后服务
            province:goodsInfo.goods.province  #发货省(ID)
            city:goodsInfo.goods.city  #发货地区(市ID)
            sendmoney:goodsInfo.goods.sendmoney #运费
            firstsort:goodsInfo.goods.firstsort #一级分类ID
            secondsort:goodsInfo.goods.secondsort #二级分类ID
            thirdsort:goodsInfo.goods.thirdsort #三级分类ID
            isnew:goodsInfo.goods.IsNew #最新上架(0否1是)
            ishot:goodsInfo.goods.IsHot #热门促销(0否1是)
            istop:goodsInfo.goods.IsTop #店长推荐(0否1是)
        formValidation: ->
            if $scope.goods.productno.match(/(^[A-Za-z0-9]+$)/) == null
                showToast '请输入正确的商品编号,由数字或字母组成.'
                return false
            else if $scope.goods.productsku.match(/^[0-9]+([.][0-9]+){0,1}$/) == null
                showToast '请输入正确的商品库存,由数字组成.'
                return false
            else if $scope.goods.saleprice.match(/^[0-9]+([.][0-9]+){0,1}$/) == null
                showToast '请输入正确的销售价,由数字组成.'
                return false
            else if $scope.goods.province == undefined
                showToast '请选择省份'
                return false
            else if $scope.goods.city == undefined
                showToast '请选择城市'
                return false
            else if $scope.goods.firstsort == undefined
                showToast '请选择分类'
                return false
            else
                return true
        goBack: ->
            $ionicViewSwitcher.nextDirection('back')
            $ionicHistory.goBack()
        getProvince:() ->
            $scope.currentCity = []
            _.each $scope.cityData,(city) ->
                if $scope.goods.province == city.provinceid
                    $scope.currentCity.push city
        getCity:() ->
        getFirst: ->
            _.each $scope.Allsort,(first)->
                if first.firstsortid == $scope.goods.firstsort
                    $scope.secondSort = first.childsort
        getSecond: ->
            _.each $scope.secnondSort,(second)->
                if second.secondsortid == $scope.goods.secondsort
                    $scope.thirdSort = second.childsort
        getThird: ->
            console.log $scope.goods.thirdsort
        changeNewOn: ->
            $scope.goods.isnew = 1
        changeNewOff: ->
            $scope.goods.isnew = 0
        changeHotOn: ->
            $scope.goods.ishot = 1
        changeHotOff: ->
            $scope.goods.ishot = 0
        changeTopOn: ->
            $scope.goods.istop = 1
        changeTopOff: ->
            $scope.goods.istop = 0
        goSave: ->
            if  $scope.formValidation()
                $ionicLoading.show()
                $http.post('/utf-8data/',$scope.goods)
                .success (resp) ->
                    if resp?.status == '0'
                        showToast '保存成功'
                        $ionicViewSwitcher.nextDirection('back')
                        $ionicHistory.goBack()
                    else
                        showToast resp.msg
                .error ->
                    showToast '网络异常,请检查网络是否连接'
                .finally ->
                    $ionicLoading.hide()
    $scope.$on '$ionicView.enter', ->
        getareaData()
        getSort()
